<template>
  <div class="banner">
    <div class="carousel">
      <PageSwipt :data="SwiptImg" />
      <div class="textinput">
        <input
          class="inputsize"
          type="text"
          placeholder="请输入需要搜索的攻略内容"
          v-model="value"
          @click="value = ''"
        />

        <a class="searchsize" href="#tj"
          ><i class="el-icon-search" @click="handleSearch"></i
        ></a>
        <div class="recommend">
          <span>推荐：</span>
          <div
            v-for="(recommend, index) in Recommend"
            :key="index"
            class="floatleft"
          >
            <a href="#tj" @click="handleRecommend(recommend)">{{
              recommend
            }}</a>
          </div>
        </div>
      </div>
    </div>
    <div class="Article">
      <div>
        <Pagecity @cityData="emitCity" />
      </div>
      <div class="right">
        <div class="topnav">
          <div class="flex" id="tj">推荐攻略</div>
          <div class="flex handlepublish">
            <div @click="handlePublish">
              发表游记
              <i class="el-icon-edit Pen"></i>
            </div>
          </div>
        </div>
        <div class="postList">
          <div
            class="articlelist"
            v-for="(item, index) in articleListdata"
            :key="index"
          >
            <ul>
              <li @click="handleArticle(item.id)">
                <div class="bottomborder">
                  <h4 v-if="item.title.length > 1">
                    <a href="#">{{ item.title }}</a>
                  </h4>

                  <p v-if="item.summary.length > 1">
                    {{ item.summary }}
                  </p>

                  <div class="images">
                    <div v-if="item.images.length >= 3" class="pictures">
                      <img :src="item.images[0]" alt="" />
                      <img :src="item.images[1]" alt="" />
                      <img :src="item.images[2]" alt="" />
                    </div>

                    <div
                      v-else-if="(item.images.length = 0)"
                      class="picture"
                    ></div>
                  </div>

                  <div class="message">
                    <div class="left">
                      <div
                        class="location el-icon-location-outline"
                        v-if="item.content != ''"
                      >
                        &nbsp;{{ item.cityName }}
                      </div>
                      <div class="user">
                        by&nbsp;
                        <img
                          :src="`${$axios.defaults.baseURL}${item.account.defaultAvatar}`"
                          alt=""
                          class="userimg"
                        />
                        {{ item.account.nickname }}
                      </div>
                      <div class="amount el-icon-thumb">
                        &nbsp;{{ item.watch }}
                      </div>
                      <div class="like">
                        <div v-if="item.like">{{ item.like }} 赞</div>
                        <div v-else>0 赞</div>
                      </div>
                    </div>
                  </div>
                </div>
              </li>
            </ul>
          </div>
        </div>
        <div class="block">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page.sync="currentPage3"
            :page-size="pageSize"
            layout="prev, pager, next, jumper"
            :total="articleList.length"
            prop="index"
          >
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import PageSwipt from "@/components/post/PageSwipt";
import Pagecity from "@/components/post/Pagecity";
export default {
  components: { PageSwipt, Pagecity },
  data() {
    return {
      value: "",
      SwiptImg: [],
      Recommend: ["广州", "成都", "北京"],
      articleList: [],
      currentPage3: 1,
      pageSize: 3,
      articleListdata: [],
    };
  },
  created() {
    this.getRaidersdata();
    this.handleRecommend();
    this.emitCity();
  },
  methods: {
    handleSearch() {
      if (this.value == "") {
        this.$message.error("请输入内容再点击搜索");
      }
      this.$axios({
        url: "/posts",
        params: {
          city: this.value,
        },
      }).then((res) => {
        this.articleList = res.data.data;
        this.getArticleListData();
      });
    },
    handleRecommend(recommend) {
      this.value = recommend;
      this.$axios({
        url: "/posts",
        params: {
          city: this.value,
        },
      }).then((res) => {
        this.articleList = res.data.data;
        this.getArticleListData();
      });
    },
    getRaidersdata() {
      this.$axios({
        url: "/scenics/banners",
      }).then((res) => {
        this.SwiptImg = res.data.data;
      });
    },
    handlePublish() {
      this.$router.push("/post/publish");
    },

    getArticleList() {
      this.$axios({
        url: "/posts",
        params: {
          pageIndex: this.currentPage3,
          pageSize: this.pageSize,
        },
      }).then((res) => {
        this.articleList = res.data.data;
        this.articleListdata = res.data.data;
        this.getArticleListData();
      });
    },
    handleSizeChange(size) {
      this.pageSize = size;
      this.getArticleListData();
    },
    handleCurrentChange(currentPage3) {
      this.currentPage3 = currentPage3;
      this.getArticleListData();
    },
    getArticleListData() {
      let num = (this.currentPage3 - 1) * this.pageSize;
      let end = num + this.pageSize;
      this.articleListdata = this.articleList.slice(num, end);
    },
    handleArticle(id) {
      this.$router.push("/post/detail?id=" + id);
    },
    emitCity(data) {
      this.$axios({
        url: "/posts",
        params: {
          city: data,
        },
      }).then((res) => {
        this.articleList = res.data.data;
        this.getArticleListData();
        this.value = data;
      });
    },
  },
};
</script>

<style lang="less" scoped>
body {
  background: #f2f2f2;
}
.banner {
  .carousel {
    position: relative;
    .textinput {
      position: absolute;
      left: 50%;
      top: 50%;
      margin: -31px 0 0 -301px;
      z-index: 2;
      .inputsize {
        width: 600px;
        height: 43px;
        padding-left: 10px;
        border-radius: 23px;
        outline: none;
        border: 3px solid orange;
      }
      .searchsize {
        display: block;
        position: absolute;
        top: 0;
        right: 9px;
        line-height: 51px;
        font-size: 28px;
        color: orange;
        cursor: pointer;
      }
      .recommend {
        padding-left: 8px;
        margin-top: 6px;
        font-size: 14px;
        color: #fff;
        span {
          float: left;
        }
        .floatleft {
          float: left;
          a {
            padding: 0 10px;
          }
          a:hover {
            color: orange;
          }
        }
      }
    }
  }

  .Article {
    position: relative;
    display: flex;
    width: 1005px;
    margin: 0 auto;
    padding: 10px 0;
    justify-content: space-between;

    .right {
      width: 740px;
      .topnav {
        height: 35px;
        line-height: 35px;
        display: flex;
        border-bottom: 1px solid #d3d3d3;

        .flex {
          background: #f1f1f1;
          flex: 1;
          text-align: center;
          color: #666;
        }
        div.handlepublish a {
          background: #f2f2f2;
          position: relative;
          display: block;
          cursor: pointer;
        }
        .pen {
          position: absolute;
          right: 0;
        }
        div.handlepublish div:hover {
          color: orange;
          cursor: pointer;
        }
      }
      .block {
        position: absolute;
        bottom: 0;
        left: 37%;
      }
    }
  }
  .articlelist {
    position: relative;
    bottom: 0;
    width: 100%;
    padding: 10px 0 10px 0px;
    ul {
      li {
        width: 100%;
        height: 100%;
        .bottomborder {
          border-bottom: 1px solid orange;
          margin-bottom: 18px;
        }
      }
    }
    cursor: pointer;
    h4 {
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      margin-bottom: 15px;
      font-weight: 400;
      font-size: 18px;
      a {
        padding-left: 18px;
        text-decoration: none;
        color: inherit;
      }
      a:hover {
        color: #888;
      }
    }
    p {
      padding: 0px 20px;
      display: -webkit-box;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 3;
      overflow: hidden;
    }
    p:hover {
      color: #888;
    }
    .images {
      .pictures {
        display: flex;
        flex-wrap: nowrap;
        width: 88%;
        height: 150px;
        padding-top: 12px;
        img {
          margin-left: 18px;
          width: 33.33%;
          height: 100%;
        }
      }
      .picture {
        width: 0;
        height: 0;
      }
    }
    .message {
      margin-left: 18px;
      margin-top: 18px;
      .left {
        display: flex;
        width: 666px;
        padding-top: 10px;

        .location,
        .user,
        .amount {
          font-size: 12px;
          margin-right: 18px;
          line-height: 22px;
        }
        .userimg {
          width: 16px;
          height: 16px;
          vertical-align: middle;
          border-radius: 100px;
        }
        .location:hover,
        .user:hover,
        .amount:hover {
          color: orange;
        }
        .like {
          position: absolute;
          right: 7px;
          color: orange;
        }
      }
    }
  }
}
</style>